<template>
	<view>
		<!-- 自定义搜索组件 -->
		<!-- <my-search :bgColor="'#000000'" :radius="3"></my-search> -->
		<!-- 由于无法直接调用自定义组件事件，需要在子组件给$emit调用 -->
		<my-search @myclick='goToSearch()'></my-search>
		<!-- 滑动区域 -->
		<view class="scroll-view-container">
			<!-- 左侧滑动区域  -->
			<scroll-view class="left-scroll-view" scroll-y="true" :style="{height:wh+'px'}">
				<block v-for="(item,index) in cateListData" :key="index">
					<view :class="['left-scroll-view-item',index===cateListActive?'active':'']" @click="clickActiveChange(index)">{{item.cat_name}}</view>
				</block>
			</scroll-view>
			<!-- 右侧滑动区域 -->
			<scroll-view class="right-scroll-view" scroll-y="true" :style="{height:wh+'px'}" :scroll-top="scrollTop">
				<view class="cate-right-scroll" v-for="(item,index) in cateListRightData" :key="index">
					<view class="cate-right-scroll-title">/ {{item.cat_name}} /</view>
					<view class="cate-right-scroll-list">
						<view class="cate-right-scroll-item" v-for="(itemList,indexList) in item.children" :key="indexList" @click="goGoodsList(itemList)">
							<image :src="itemList.cat_icon"></image>
							<text>{{itemList.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	// 导入maxins--自定义tabBar购物车右上角数字徽标组件
	import badgeMix from '@/mixins/maxins.js'
	
	export default {
		// 使用自定义maxins
		mixins:[badgeMix],
		data() {
			return {
				// 当前设备可用的屏幕高度
				wh:0,
				// 分类数据
				cateListData:[],
				// 分类数据索引号
				cateListActive:0,
				//右侧二级分类
				cateListRightData:[],
				//右侧分类滚动刷新
				scrollTop:0
			};
		},
		methods:{
			// 获取分类数据
			async getCateListData(){
				const {data:res} = await uni.$http.get('/api/public/v1/categories')
				if(res.meta.status!==200){
					return uni.$showMsg()
				}
				// 左侧数据
				this.cateListData=res.message
				// 右侧数据
				this.cateListRightData=res.message[0].children
			},
			// 修改分类数据索引Active
			clickActiveChange(index){
				// 定位分类
				this.cateListActive=index
				// 修改右侧数据
				this.cateListRightData=this.cateListData[index].children
				// 刷新后重新回到顶部
				this.scrollTop=this.scrollTop===0?1:0
			},
			// 点击商品的跳转
			goGoodsList(itemList){
				uni.navigateTo({
					url:'/subPkg/goods_list/goods_list?cid='+itemList.cat_id
				})
			},
			// 搜索跳转
			goToSearch(){
				uni.navigateTo({
					url:'/subPkg/search/search'
				})
			}
		},
		onLoad() {
			// 获取屏幕高度
			const sysInfo = uni.getWindowInfo()
			this.wh=sysInfo.windowHeight - 50
			// 调用获取分类数据
			this.getCateListData()
		}
	}
</script>

<style lang="scss">
	.scroll-view-container{
		display: flex;
		.left-scroll-view{
			width: 120px;
			.left-scroll-view-item{
				background-color: #f7f7f7;
				line-height: 60px;
				text-align: center;
				font-size: 12px;
				&.active{
					background-color:#ffffff;
					position: relative;
					&::before{
						content: ' ';
						display: block;
						width: 3px;
						height: 0px;
						background-color: red;
						position: absolute;
						top: 50%;
						left: 0;
						transform: translateY(-50%);
						transition:all 1s ease-in;
						animation: activeAnimat .3s ease forwards; 
					}
				}
			}
		}
		.cate-right-scroll-title{
			font-size: 12px;
			font-weight: bold;
			text-align: center;
			padding: 15px;
		}
		.cate-right-scroll-list{
			display: flex;
			flex-wrap: wrap;
			.cate-right-scroll-item{
				width: 33.33%;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				margin-bottom: 10px;
				image{
					width: 60px;
					height: 60px;
				}
				text{
					font-size: 12px
				}
			}
		}
	}
	@keyframes activeAnimat {
		0%{
			height:0px;
		}
		100%{
			height:30px;
		}
	}
</style>
